<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>新增页面</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />

</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container mr-5">
            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label required" >id</label>
                <div class="layui-input-block">
                    <input type="text" name="id" value="" class="layui-input" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="product_name" value="" class="layui-input" disabled>
                </div>
            </div>
            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label required">描述</label>
                <div class="layui-input-block">
                    <input type="text" name="product_des" value="" class="layui-input" disabled>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">价格</label>
                <div class="layui-input-block">
                    <input type="number" name="product_price" value="" class="layui-input" disabled>
                </div>
            </div>
            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label required">类型</label>
                <div class="layui-input-block">
                    <input type="number" name="product_type" value="" class="layui-input" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">激活码</label>
                <div class="layui-input-block">
                    <textarea name="product_activated_code" placeholder="请输入激活码以;间隔,注意中英文区分；例如：3355abvdsdrew^;iiujy780982;889901234" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-md">
                重置
            </button>
        </div>
    </div>

</form>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>

<script>
    // 相关接口
    const PRIMARY_KEY = "id";
    const SELECT_API = "/app/admin/product/select" + location.search;
    const UPDATE_API = "/app/admin/product/update";
    const QUERY_PRODUCT_TYPE = "/app/admin/product/query_product_type";
    const DELETE_API = "/app/admin/product/delete";
    const add_activated_code = "/app/admin/product/add_activated_code";
    let product_id = ""
    let product_type = ""
    // 获取数据库记录
    layui.use(["form", "util", "popup"], function () {
        let $ = layui.$;
        deleteData = []
        $.ajax({
            url: SELECT_API,
            dataType: "json",
            success: function (res) {
                console.log("输出结果 res.data：",res.data)
                console.log("输出结果 res.data[0] ：",res.data[0])
                deleteData.push(res.data[0])
                product_id = res.data[0].id
                product_type = res.data[0].product_type
                console.log("产品ID",product_id)
                console.log("产品Type",product_type)

                // 给表单初始化数据
                layui.each(res.data[0], function (key, value) {
                    let obj = $('*[name="'+key+'"]');
                    if (key === "password") {
                        obj.attr("placeholder", "不更新密码请留空");
                        return;
                    }
                    if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                    if (obj[0].nodeName.toLowerCase() === "textarea") {
                        obj.val(value);
                    } else {
                        obj.attr("value", value);
                        obj[0].value = value;
                    }
                });


                // ajax返回失败
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    //提交事件
    layui.use(["form", "popup"], function () {
        layui.form.on("submit(save)", function (data) {
            data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
            if (!data.field.product_name){
                layer.msg("请输入产品名称")
                return false
            }
            if (!data.field.product_des){
                layer.msg("请添加产品描述")
                return false
            }
            if (!data.field.product_des){
                layer.msg("请添加产品价格")
                return false
            }
            data.field.id = product_id
            data.field.product_type = product_type
            console.log("新增激活码页面输出的表单数据：",data.field)
            // 发送 AJAX 请求前显示加载遮罩
            let loadingIndex = layui.layer.load(2, {shade: [0.3, '#fff']}); // 0.3透明度的白色背景
            layui.$.ajax({
                url: add_activated_code,
                type: "POST",
                dateType: "json",
                data: data.field,
                success: function (res) {
                    // 请求成功后隐藏加载遮罩
                    layui.layer.close(loadingIndex);
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功", function () {
                        parent.refreshTable();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                },
                error: function () {
                    // 请求失败也要隐藏加载遮罩
                    layui.layer.close(loadingIndex);
                    // 处理错误情况，比如提示用户请求失败
                    layui.layer.msg("请求失败，请稍后重试", {icon: 2});
                }
            });
            return false;
        });
    });

</script>

</body>

</html>
